package com.ruoyi.eduadmin.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.eduadmin.domain.TRepetition;
import com.ruoyi.eduadmin.mapper.TRepetitionMapper;
import com.ruoyi.eduadmin.service.ITRepetitionService;

import com.ruoyi.student.api.StudentService;
import com.ruoyi.student.api.domain.TStudent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 留级管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-12-30
 */
@Service
public class TRepetitionServiceImpl implements ITRepetitionService 
{
    @Autowired
    private TRepetitionMapper tRepetitionMapper;

    @Autowired
    private StudentService studentService;

//    @Autowired
//    private ClassService classService;

    /**
     * 查询留级管理
     * 
     * @param id 留级管理主键
     * @return 留级管理
     */
    @Override
    public TRepetition selectTRepetitionById(Long id)
    {
        return tRepetitionMapper.selectTRepetitionById(id);
    }

    /**
     * 查询留级管理列表
     * 
     * @param tRepetition 留级管理
     * @return 留级管理
     */
    @Override
    public List<TRepetition> selectTRepetitionList(TRepetition tRepetition)
    {

        return tRepetitionMapper.selectTRepetitionList(tRepetition);
    }

    /**
     * 新增留级管理
     * 
     * @param tRepetition 留级管理
     * @return 结果
     */
    @Override
    public int insertTRepetition(TRepetition tRepetition)
    {
        AjaxResult ajaxResult = studentService.findStudentByName(tRepetition.getStudentName());
        if (!ajaxResult.isSuccess()){
            throw new ServiceException("获取失败！");
        }
        Object data = ajaxResult.get("data");
        if (data == null){
            throw new ServiceException("获取失败！");
        }
        TStudent tStudent = JSONObject.parseObject(JSONObject.toJSONString(data), TStudent.class);

        tRepetition.setStudentId(tStudent.getId());
        return tRepetitionMapper.insertTRepetition(tRepetition);
    }

    /**
     * 修改留级管理
     * 
     * @param tRepetition 留级管理
     * @return 结果
     */
    @Override
    public int updateTRepetition(TRepetition tRepetition)
    {
        return tRepetitionMapper.updateTRepetition(tRepetition);
    }

    /**
     * 批量删除留级管理
     * 
     * @param ids 需要删除的留级管理主键
     * @return 结果
     */
    @Override
    public int deleteTRepetitionByIds(Long[] ids)
    {
        return tRepetitionMapper.deleteTRepetitionByIds(ids);
    }

    /**
     * 删除留级管理信息
     * 
     * @param id 留级管理主键
     * @return 结果
     */
    @Override
    public int deleteTRepetitionById(Long id)
    {
        return tRepetitionMapper.deleteTRepetitionById(id);
    }

    @Override
    public TRepetition selectTraceTRepetitionList(Long id) {

        return tRepetitionMapper.selectTraceTRepetitionList(id);
    }
}
